package com.example.lianjuan.ui.frament;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lianjuan.Base.BaseFragment;
import com.example.lianjuan.Base.IBaseInfo;
import com.example.lianjuan.Model.domain.SelectCategories;
import com.example.lianjuan.Model.domain.SelectContent;
import com.example.lianjuan.Presenter.impl.impl.SelectPresenterImpl;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.PresenterManang;
import com.example.lianjuan.Utils.SizeUtils;
import com.example.lianjuan.Utils.TicketUtil;
import com.example.lianjuan.View.ISelectCallback;
import com.example.lianjuan.ui.adapter.PagerAdapter;
import com.example.lianjuan.ui.adapter.selectAdapter;

import java.util.List;

public class SelectFragment extends BaseFragment implements ISelectCallback, selectAdapter.onCategoryItemClickListener, PagerAdapter.OnClickItemListener {

    private SelectPresenterImpl mSelectPresenterImpl;
    private RecyclerView mSelectTv, mPagerTv;
    private selectAdapter mSelectAdapter;
    private PagerAdapter mPagerAdapter;
    private LinearLayoutManager mLinearLayoutManager;


    @Override
    protected void initView(View view) {
        mSelectTv = view.findViewById(R.id.select_recycler_view);
        mPagerTv = view.findViewById(R.id.select_pager_recycler_view);
        mSelectAdapter = new selectAdapter();
        mPagerAdapter = new PagerAdapter();
        mSelectTv.setLayoutManager(new LinearLayoutManager(getContext()));
        mSelectTv.setAdapter(mSelectAdapter);
        //只对右边的ReclcerView进行管理，切换是回到顶部
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mPagerTv.setLayoutManager(mLinearLayoutManager);
        mPagerTv.setAdapter(mPagerAdapter);
        //设置RecyclerView外间距
        mPagerTv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(getContext(), 4);
                int liftAndRight = SizeUtils.dip2px(getContext(), 3);
                outRect.top = topAndBottom;
                outRect.bottom = topAndBottom;
                outRect.left = liftAndRight;
                outRect.right = liftAndRight;
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mSelectAdapter.setCategoryItemClickListener(this);
        mPagerAdapter.setClickItemListener(this);
        //网络错误点击重新加载
        networkClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectPresenterImpl != null) {
                    mSelectPresenterImpl.getSelectCategories();
                }
            }
        });
    }

    @Override
    protected void release() {
        super.release();
        //取消注册
        mSelectPresenterImpl.unregisterViewCallback(this);
    }

    @Override
    protected void loadData() {
        mSelectPresenterImpl.getSelectCategories();
    }

    @Override
    protected void initPresenter() {
        mSelectPresenterImpl = PresenterManang.getInstance().getSelectPresenter();
        mSelectPresenterImpl.registerViewCallback(this);
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_select_bar_layout, container, false);
    }

    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_select;
    }

    @Override
    public void onNetworkError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoadEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    public void onLoadNone() {
    }

    @Override
    public void onLoadLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onLoadError() {
        setUpState(State.ERROR);
    }

    @Override
    public void getSelectCategories(SelectCategories item) {
        List<SelectCategories.DataDTO> categoriesContent = item.getData();
        if (categoriesContent != null) {
            mSelectAdapter.setSelectData(categoriesContent);
            LogUtils.d(this, "categoriesContent------>" + categoriesContent.toString());
        }
        setUpState(State.SUCCESS);
    }

    @Override
    public void getSelectContent(SelectContent content) {
        mPagerAdapter.setData(content);
        //左侧Recycle被点击后,右侧Releycle回到第一个
        mPagerTv.scrollToPosition(0);
    }

    @Override
    public void onCategoryItemClick(SelectCategories.DataDTO item) {
        mSelectPresenterImpl.getContentByCategoriesID(item);
        //TODO 得到点击的数据
        LogUtils.d(this, "item--->" + item.toString());
    }

    //PagerAdapter点击事件回调
    @Override
    public void onClickItem(IBaseInfo iBaseInfo) {
        //跳转到优惠价界面
        TicketUtil.toTickerPager(getContext(), iBaseInfo);
    }

}
